import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;

import javax.swing.JPanel;

public class Figure {
	Point startPoint, curPoint, movePoint, pPoint;
	ArrayList<Line> lines = new ArrayList<Line>();
	ArrayList<Point> points = new ArrayList<Point>();
	public Figure (Point p0) { 
		startPoint = new Point(p0.x, p0.y);
		pPoint = startPoint;
		curPoint = p0;
		points.add(new Point (p0.x, p0.y));
	}
	public void addLine (Point p) {
		Line newLine = new Line (curPoint, p);
		curPoint = p;
		lines.add(newLine);
	}
	public ArrayList<Line> getLines(){ return lines;}
	public ArrayList<Point> getPoints(){ return points;}
	public Point popPoint() {
		Point ret = points.get(points.size()-1);
		points.remove(ret);
		if (points.size() > 0) pPoint = points.get(points.size()-1);
		else pPoint = startPoint;
		return ret;
	}
	public void pushPoint (){ 
		points.add(new Point (lines.get(0).start.x, lines.get(0).start.y));
	}
	public void move (int dx, int dy){
		for (int i = 0; i < lines.size(); i++) {
			int st = 0;
			if (i == 0) {
				st = dx;
			}
			lines.get(i).move(dx, dy, i == 0);
		}
	}
	public void move (Point p) {
		int dx = p.x - movePoint.x;
    	int dy = p.y - movePoint.y;
    	this.move(dx, dy);
    	movePoint = p;
		
	}
	public void move (Point st, Point end) {
		int dx = end.x - st.x;
		int dy = end.y - st.y;
		this.move(dx, dy);
	}
	public float getMinDist (Point p) {
		float ret = 10000;
		Line nearL = null;
		for (int i = 0; i < lines.size(); i ++){
			float dist = lines.get(i).distToPoint(p);
			if (ret > dist) {
				ret = dist;
				nearL = lines.get(i);
			}
		} 
		//if (nearL != null) nearL.color = Color.RED;
		return ret;
	}
	public void setMovePoint (Point p) { movePoint = p; }
	public void setColor (Color col) { for (Line each: lines) each.color=col; }
}
